package org.real.guetshop.controller.admin;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.real.guetshop.pojo.dto.DetailDTO;
import org.real.guetshop.pojo.dto.MerchantDTO;
import org.real.guetshop.pojo.dto.MerchantLoginDTO;
import org.real.guetshop.pojo.dto.MerchantRegisterDTO;
import org.real.guetshop.pojo.entity.Store;
import org.real.guetshop.pojo.vo.MerchantLoginVO;
import org.real.guetshop.pojo.vo.StoreVO;
import org.real.guetshop.propertis.JwtProperties;
import org.real.guetshop.result.Result;
import org.real.guetshop.service.MerchantService;
import org.real.guetshop.utils.JwtUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController("adminMerchantController")
@RequestMapping("/admin/merchant")
@Slf4j
public class MerchantController {

    @Resource
    private MerchantService merchantService;

    @Resource
    private JwtProperties jwtProperties;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 商家注册
     * @param merchantRegisterDTO
     * @return
     */
    @PostMapping("/register")
    @Operation(summary = "商家注册")
    public Result<String> register(@RequestBody MerchantRegisterDTO merchantRegisterDTO){
        log.info("商家注册：{}",merchantRegisterDTO);

        merchantService.register(merchantRegisterDTO);

        //删除缓存中的数据
        redisTemplate.delete("user_merchant_list");

        return Result.success(merchantRegisterDTO.getUsername());
    }

    /**
     * 补充详细信息
     * @param detailDTO
     * @return
     */
    @PostMapping("/detail")
    @Operation(summary = "补充详细信息")
    public Result detail(@RequestBody DetailDTO detailDTO){
        log.info("商家信息补充：{}",detailDTO);
        merchantService.detail(detailDTO);
        return Result.success();
    }

    /**
     * 商家信息修改
     * @param merchantDTO
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody MerchantDTO merchantDTO){
        log.info("开始修改商家信息：{}",merchantDTO);
        merchantService.update(merchantDTO);
        return Result.success("商家信息修改成功！");
    }

    @GetMapping("/getStore/{storeId}")
    public Result<StoreVO> getStore(@PathVariable Integer storeId){
        log.info("商家获取商家信息：{}",storeId);
        return Result.success(merchantService.getStoreInfo(storeId));
    }


    /**
     * 商家登录
     * @param merchantLoginDTO
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "商家登录")
    public Result<MerchantLoginVO> login(@RequestBody MerchantLoginDTO merchantLoginDTO){
        log.info("商家登录：{}",merchantLoginDTO);

        Store store = merchantService.login(merchantLoginDTO);

        //获得当前时间的秒数
        long nowTime = System.currentTimeMillis();

        String token = JwtUtil.createToken(store.getId()
                , new Date(nowTime)
                , new Date(nowTime + jwtProperties.getAdminTtl()));

        MerchantLoginVO merchantLoginVO = MerchantLoginVO.builder()
                .id(store.getId())
                .username(store.getUsername())
                .name(store.getName())
                .address(store.getAddress())
                .phone(store.getPhone())
                .status(store.getStatus())
                .token(token)
                .build();

        return Result.success(merchantLoginVO);
    }
}
